package com.svms.grid.pagin;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.Validator;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.bind.validator.AbstractValidator;
import org.zkoss.zul.ListModelList;

import com.svms.service.util.SvmsDateUtils;
import com.svms.service.vo.MovementVO;
import com.svms.service.vo.VehicleVO;
import com.svms.web.factory.SvmsWebFactory;

/**
 * @author om39a
 * 
 */
public class ManageMovementViewModel {

	// <---------------------------------------------------------------------------------->
	// Variable Declarations
	// <---------------------------------------------------------------------------------->

	ListModelList<MovementVO> movements;
	ListModelList<VehicleVO> vehicles;
	String confirmSaveMessage;
	String confirmDeleteMessage;
	MovementVO selectedMovement;
	String addInProgress;

	public MovementVO getSelectedMovement() {
		return selectedMovement;
	}

	public void setSelectedMovement(MovementVO selectedMovement) {
		this.selectedMovement = selectedMovement;
	}

	public String getConfirmSaveMessage() {
		return confirmSaveMessage;
	}

	public String getConfirmDeleteMessage() {
		return confirmDeleteMessage;
	}

	public String getAddInProgress() {
		return addInProgress;
	}

	public void setAddInProgress(String addInProgress) {
		this.addInProgress = addInProgress;
	}

	// <---------------------------------------------------------------------------------->
	// Fetch Data from DB
	// <---------------------------------------------------------------------------------->

	public ListModelList<MovementVO> getAllMovementInfo() {

		movements = new ListModelList<MovementVO>(SvmsWebFactory
				.movementService().getMovementList());
		return movements;
	}

	public ListModelList<VehicleVO> getAllVehicleInfo() {
		vehicles = new ListModelList<VehicleVO>(SvmsWebFactory.vehicleService()
				.getVehicleList());
		return vehicles;
	}

	// <---------------------------------------------------------------------------------->
	// Command Buttons actions
	// <---------------------------------------------------------------------------------->

	@Command
	@NotifyChange({ "selectedMovement", "movements", "addInProgress" })
	public void addNewMovement() {
		MovementVO movementVO = new MovementVO();
		selectedMovement = movementVO;
		setAddInProgress("Add In Progress");

		System.out.println("-------------------------------------");
		System.out.println("New Movement Dialogue Invoked");
		System.out.println("Selected Movement ===> " + selectedMovement);
		System.out.println("-------------------------------------");
	}

	@Command
	@NotifyChange({ "selectedMovement", "movements", "addInProgress" })
	public void discardAddNewMovement() {
		movements.remove(selectedMovement);

		selectedMovement = null;
		setAddInProgress(null);

		System.out.println("-------------------------------------");
		System.out.println("New Movement Dialogue Invoked");
		System.out.println("Selected Movement ===> " + selectedMovement);
		System.out.println("-------------------------------------");
	}

	@Command
	@NotifyChange({ "selectedMovement", "movements", "confirmSaveMessage",
			"addInProgress" })
	public void saveNewMovement() {

		System.out.println("-------------------------------------");
		System.out.println("Save Movement Dialogue Invoked");
		System.out.println("Selected Movement Before ===> " + selectedMovement);

		MovementVO movementFromDb;
		if (StringUtils.isNotBlank(addInProgress)) {
			movementFromDb = SvmsWebFactory.movementService()
					.insertNewMovement(selectedMovement);
			if ("SUCCESS".equalsIgnoreCase(movementFromDb.getResultCode()
					.getResult().get("Result"))) {
				movements.add(movementFromDb);
			}
		} else {
			movementFromDb = SvmsWebFactory.movementService().editMovement(
					selectedMovement);
		}
		confirmSaveMessage = null;
		setAddInProgress(null);
		System.out.println("Selected Movement After ===> " + selectedMovement);
		System.out.println("-------------------------------------");
	}

	@Command
	@NotifyChange({ "confirmSaveMessage" })
	public void confirmSaveMovement() {

		System.out.println("-------------------------------------");
		System.out.println("Save Movement Dialogue Invoked");
		System.out.println("Selected Movement Before ===> " + selectedMovement);

		confirmSaveMessage = "Are you sure you want to Save the Movement";

		System.out.println("-------------------------------------");
		System.out.println("New Confirm Save Movement Dialogue Invoked");
		System.out.println("Selected Movement After ===> " + selectedMovement);
		System.out.println("-------------------------------------");
	}

	@Command
	@NotifyChange({ "selectedMovement", "movements", "confirmDeleteMessage" })
	public void deleteMovement() {

		MovementVO result = SvmsWebFactory.movementService().deleteMovement(
				selectedMovement);
		System.out.println(result.getResultCode().getResult());

		movements.remove(selectedMovement);
		selectedMovement = null;
		confirmDeleteMessage = null;

		System.out.println("-------------------------------------");
		System.out.println("New Delete Movement Dialogue Invoked");
		System.out.println("Selected Movement ===> " + selectedMovement);
		System.out.println("-------------------------------------");
	}

	@Command
	@NotifyChange({ "confirmDeleteMessage" })
	public void confirmDeleteMovement() {

		confirmDeleteMessage = "Are you sure you want to Delete the Movement";
		/*
		 * + selectedMovement.getVehicleVO().getVehicleId() + " " +
		 * selectedMovement.getStartPlace() + " " +
		 * selectedMovement.getEndPlace();
		 */
		System.out.println("-------------------------------------");
		System.out.println("New Confirm Delete Movement Dialogue Invoked");
		System.out.println("Selected Movement ===> " + selectedMovement);
		System.out.println("-------------------------------------");
	}

	@Command
	@NotifyChange({ "confirmDeleteMessage", "confirmSaveMessage" })
	public void cancleCRUDStatus() {
		System.out.println("-------------------------------------");
		System.out.println("Cancle CURD status Movement Dialogue Invoked");
		System.out.println("Selected Movement Before ===> " + selectedMovement);
		confirmDeleteMessage = null;
		confirmSaveMessage = null;
		System.out.println("Selected Movement after ===> " + selectedMovement);
		System.out.println("-------------------------------------");
	}

	@Command
	public void clickOnList() {
		System.out.println("-------------------------------------");
		System.out.println("On selecting the list Invoked");
		System.out.println("Selected Movement after ===> " + selectedMovement);
		System.out.println("selectedMovement.getVehicle()==> "
				+ selectedMovement.getVehicleVO());
		System.out.println("-------------------------------------");
	}

	// <---------------------------------------------------------------------------------->
	// Validations
	// <---------------------------------------------------------------------------------->

	public Validator getValidateVehicleId() {
		System.out.println("==>> VehicleValidator");
		return new AbstractValidator() {
			public void validate(ValidationContext ctx) {
				String vehicleId = (String) ctx.getProperty().getValue();
				System.out.println("------> " + vehicleId);
				if (StringUtils.isBlank(vehicleId)) {
					addInvalidMessage(ctx, "Movement ID Must contain a value");
				}
			}
		};
	}

	public Validator getValidateStartDate() {
		System.out.println("==>> MovementValidator");
		return new AbstractValidator() {
			public void validate(ValidationContext ctx) {
				Date selectedDate = (Date) ctx.getProperty().getValue();
				System.out.println("------> " + selectedDate);
				if (selectedDate == null) {
					addInvalidMessage(ctx, "Start Date Must contain a value");
				} else if (SvmsDateUtils.isBeforeDay(selectedDate, new Date())) {
					addInvalidMessage(ctx, "Selected a future date");
				}

			}
		};
	}

	public Validator getValidateEndDate() {
		System.out.println("==>> MovementValidator");
		return new AbstractValidator() {
			public void validate(ValidationContext ctx) {
				Date selectedDate = (Date) ctx.getProperty().getValue();
				System.out.println("------> " + selectedDate);
				if (selectedDate == null) {
					addInvalidMessage(ctx, "End Date Must contain a value");
				} else if (SvmsDateUtils.isBeforeDay(selectedDate, new Date())) {
					addInvalidMessage(ctx, "Selected a future date");
				}

			}
		};
	}

}